# 将罗马数字转成十进制数字。同样要考虑一下4 9 40 90 400 900 特殊情况
# 思路，逆序遍历。根据后面两个的比较结果决定sum是加还是减。
class Solution:
    romanDict={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
    def romanToInt(self,s:str)->int:
        sum=0
        first=0
        second=0
        j=len(s)-1
        if s == "":
            return 0

        while j>=1:
            first=self.romanDict[s[j]]
            second=self.romanDict[s[j-1]]
            if first<=second:
                sum+=first
                j-=1
            else:
                sum=sum+first-second
                j-=2

        if j==0:
            sum+=self.romanDict[s[j]]
        return sum
solution = Solution()
print(solution.romanToInt("LVIII"))

# AC
# Runtime: 52 ms, faster than 98.34% of Python3 online submissions for Roman to Integer.
# Memory Usage: 13.3 MB, less than 53.98% of Python3 online submissions for Roman to Integer.
